package com.yanqu.road.dao.impl.activity.deepfish;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.deepfishing.DeepFishingUserFishData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DeepFishingFishDataDaoImpl extends TempDataObjectDao<DeepFishingUserFishData> {
    public static String getDeepFishingUserFishDataSql = "select * from `t_u_deep_fishing_fish_data` where `activityId`=? and `userId`=?";
    public Map<Integer, DeepFishingUserFishData> getDeepFishingUserFishDataMap(int activityId,long userId) {
        Map<Integer, DeepFishingUserFishData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDeepFishingUserFishDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                DeepFishingUserFishData parse = parse(rs);
                dataMap.put(parse.getFishId(),parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateDeepFishingUserFishDataSql = "update `t_u_deep_fishing_fish_data` set num=?,heaviestWeight=?,isReadSample=? where activityId=? and userId=? and fishId=?";
    @Override
    protected boolean updateData(DeepFishingUserFishData deepFishingUserFishData) {
        deepFishingUserFishData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserFishData.getNum()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserFishData.getHeaviestWeight()));
        para.put(i++, new DbParameter(Types.BOOLEAN, deepFishingUserFishData.getIsReadSample()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserFishData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserFishData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserFishData.getFishId()));
        if (getDb().executeNoneQuery(updateDeepFishingUserFishDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDeepFishingUserFishDataSql = "insert into `t_u_deep_fishing_fish_data`(activityId,userId,fishId,num,heaviestWeight,isReadSample) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(DeepFishingUserFishData deepFishingUserFishData) {
        deepFishingUserFishData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserFishData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, deepFishingUserFishData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserFishData.getFishId()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserFishData.getNum()));
        para.put(i++, new DbParameter(Types.INTEGER, deepFishingUserFishData.getHeaviestWeight()));
        para.put(i++, new DbParameter(Types.BOOLEAN, deepFishingUserFishData.getIsReadSample()));
        if (getDb().executeNoneQuery(insertDeepFishingUserFishDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DeepFishingUserFishData parseData(ResultSet rs) throws SQLException {
        DeepFishingUserFishData deepFishingUserFishData = new DeepFishingUserFishData();
        deepFishingUserFishData.setActivityId(rs.getInt("activityId"));
        deepFishingUserFishData.setUserId(rs.getLong("userId"));
        deepFishingUserFishData.setFishId(rs.getInt("fishId"));
        deepFishingUserFishData.setNum(rs.getInt("num"));
        deepFishingUserFishData.setHeaviestWeight(rs.getInt("heaviestWeight"));
        deepFishingUserFishData.setIsReadSample(rs.getBoolean("isReadSample"));

        return deepFishingUserFishData;
    }

}
